package com.xio.cloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xio.cloud.domain.CommonResult;
import com.xio.cloud.domain.Payment;
import com.xio.cloud.handler.CustomerBlockHandler;
import com.xio.cloud.service.PaymentFeignService;
import com.xio.cloud.service.fallback.PaymentFeignServiceFallBack;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @Author: xio
 * @Description: 服务消费者控制层
 * @Date: 2020/10/23 18:28
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @Value("${server.port}")
    private String serverPort;

    // 关于注解的使用 https://sentinelguard.io/zh-cn/docs/annotation-support.html
    @GetMapping("/payment/get/{id}")
    @SentinelResource(value = "orderGetPayment",
            blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handleException",
            fallbackClass = PaymentFeignServiceFallBack.class, defaultFallback = "defaultFallBack")
    // TODO fallbackClass 无效
    public CommonResult<Payment> getPayment(@PathVariable Long id) {
        log.info(" - - - - in:" + serverPort);
        if (id == 2L) {
            throw new NullPointerException();
        }
        return paymentFeignService.getPayment(id);
    }

    public CommonResult<Payment> getPaymentFallback(Long id) {
        return new CommonResult<>(207, "OrderController getPaymentFallback is error");
    }

}
